/**
 * 订单定时器间隔时间
 * 默认 5分钟
 */
var orderTimerInterval = 1000*60*5;
var orderDataSize = 0;


$(function(){
    /**
     * 首页-订单任务 定时器
     */
    window.setInterval("orderTask()",orderTimerInterval);
    orderTask();
});


/**
 * 订单任务
 */
function orderTask(){
    var orderInfoHtml = '<tr>' +
        '<td>{serialNumber}</td>' +
        '<td><div class="orderNumber">{orderNumber}</div></td>' +
        '<td><span class="glyphicon glyphicon-time table_span"></span>{createTime}</td>' +
        '{status}' +
        '<td><button class="btn btn-default table_button">受理</button></td>' +
        '</tr>'

    if( !userInfo.deptCode ){
        return;
    }

    $.ajax({
        type:"POST",
        url:OMS_SERVER_NAME + "oms-ltl-web/uaporder/queryOrder.action",
        data: {"uapOrderVO.uapOrderCondition.deptCode":userInfo.deptCode},
        dataType:"json",
        success:function(data){
            if(data && data.uapOrderVO){
                //未处理订单
                $(".order-task .module-content .data:eq(0) span").html(data.uapOrderVO.currentWaitOrderNumber);
                //今日处理订单
                $(".order-task .module-content .data:eq(1) p").html(data.uapOrderVO.currentNumber);

                //清空历史记录
                $(".order-task .module-content .data_parent-border").remove();

                var $orderList = $('<div class="data_parent-border" >' +
                    '<div class="data_border_box data_border_box_task" >' +
                    '<table class="data-table task_table">' +
                    '<tbody></tbody>' +
                    '</table>' +
                    '</div>' +
                    '</div>');

                if(data.uapOrderVO.uapOrders.length > 0){
                    $.each(data.uapOrderVO.uapOrders,function(index,item){
                        //日期处理
                        var createDate = new Date(item.createTime);
                        var time = createDate.format("yyyy-MM-dd hh:mm");
                        //当前状态处理
                        var status = new Date(createDate.getTime() + 1000 * 60 * 10) > new Date(data.currentTime) ? '<td><label class="label label-default normal_default table_label">正常</label></td>':'<td><label class="label label-danger table_label">紧急</label></td>';

                        var $info = $(orderInfoHtml.replace("{serialNumber}",index+1).replace("{orderNumber}",item.orderNumber).replace("{createTime}",time).replace("{status}",status));
                        $info.find("td div.orderNumber").attr('title',item.orderNumber);
                        //绑定 访问首页页面
                        $info.find("td button.table_button").click(function(){
                            var href = OMS_SERVER_NAME +'oms-ltl-web/uaporder/uaporder.action?orderNumber=' + item.orderNumber;
                            var viewType = "DIV";
                            var tab = getTabById(href,viewType);
                            if( tab ){
                                closeTab(href,viewType);

                            }

                            viewTab('订单处理',href,viewType);
                        });

                        $orderList.find("table tbody").append($info);
                    });

                    $(".order-task .module-content").append($orderList);
                    workOrderTask();
                }else {
                    workOrderTaskOrderTaskisNull();
                }
            }else{
                workOrderTaskOrderTaskisNull();
            }
        },
        error:function () {
            workOrderTaskOrderTaskisNull();
        }
    });
}

/**
 订单任务 查看全部
 */

//封装菜单详情
var orderLogdetail = setMenuDetail( "OMS_05002","零担订单受理","OMS",OMS_SERVER_NAME + 'oms-ltl-web/orderaccepted/accepted.action');

function orderTaskAll(){
	var orderAllUrl = OMS_SERVER_NAME + 'oms-ltl-web/orderaccepted/accepted.action';
   
	viewTab('零担订单受理',orderAllUrl,'div',null,orderLogdetail);
	
}



function workOrderTask(){
    $(".data_parent-border.complaintCls").remove();
    $.ajax({
        type:"post",
        url:CRM_SERVER_NAME+"crm-web/complaint/searchDeptComplaintList.action",
        data:{
            "condition.numValue":'',
            "page":1,
            "start":0,
            "limit":10
        },
        dataType:"json",
        success:function(data){
            if(data.success){
                //未处理订单
                var workOrderList = $('<div class="data_parent-border complaintCls">' +
                    '<div class="data_border_box data_border_box_task">' +
                    '<table class="data-table task_table_right">' +
                    '<tbody></tbody>' +
                    '</table>' +
                    '</div>' +
                    '</div>');
                var workInfoHtml ='<tr>' +
                    '<td hidden="true"><div class="complainNumber">{compId}</div></td>' +
                    '<td><div class="complainNumber">{voucherNumber}</div></td>' +
                    '<td><div class="createTime"><span class="glyphicon glyphicon-time table_span"></span>{createTime}</div></td>' +
                    '<td><div class="createName">{createName}</div></td>' +
                    '<td><div class="createType">{createType}</div></td>' +
                    '<td><div class="createGrade">{createGrade}</div></td>' +
                    '<td><lable class="label table_label {labelClass}">{classRank}</lable></td>' +
                    '</tr>';

                if(data.complaintList.length>0){
                    $(".complain-task .module-content .data span").html(data.totalCount);
                    $.each(data.complaintList,function(index,item){
                        var _PARAMS = formatCRMData(item);
                        var createDate =new Date(item.createTime);
                        var time =createDate.format("yyyy-MM-dd hh:mm");
                        var $info =$(workInfoHtml.replace("{voucherNumber}" ,item.voucherNumber)
                            .replace("{compId}",item.id)
                            .replace("{createTime}",time)
                            .replace("{createName}",item.callMan)
                            .replace("{createType}",_PARAMS[0])
                            .replace("{createGrade}",_PARAMS[1])
                            .replace("{classRank}",_PARAMS[2])
                            .replace("{labelClass}", item.priorityLevel == "HIGH" ? "label-warning" : "label-default" ));
                        $info.find("td div.complainNumber").attr('title',item.voucherNumber);
                        workOrderList.find("table tbody").append($info);
                    });
                    $(".complain-task .module-content").append(workOrderList);
                    $(".complain-null").css("display","none");
                    $(".order-null").css("display","none");
                }else{
                    $(".complain-null").css("display","block");
                }
            }else{
                $(".complain-null").css("display","block");
            }
        },
        error:function () {
            $(".complain-null").css("display","block");
        }
    });
}
function workOrderTaskOrderTaskisNull(){
    $(".data_parent-border.complaintCls").remove();
    $.ajax({
        type:"post",
        url:CRM_SERVER_NAME+"crm-web/complaint/searchDeptComplaintList.action",
        data:{
            "condition.numValue":'',
            "page":1,
            "start":0,
            "limit":10
        },
        dataType:"json",
        success:function(data){
            if(data.success){
                //未处理订单
                var workOrderList = $('<div class="data_parent-border complaintCls">' +
                    '<div class="data_border_box data_border_box_task">' +
                    '<table class="data-table task_table_right">' +
                    '<tbody></tbody>' +
                    '</table>' +
                    '</div>' +
                    '</div>');

                var workInfoHtml ='<tr>' +
                    '<td hidden="true"><div class="complainNumber">{compId}</div></td>' +
                    '<td><div class="complainNumber">{voucherNumber}</div></td>' +
                    '<td><div class="createTime"><span class="glyphicon glyphicon-time table_span"></span>{createTime}</div></td>' +
                    '<td><div class="createName">{createName}</div></td>' +
                    '<td><div class="createType">{createType}</div></td>' +
                    '<td><div class="createGrade">{createGrade}</div></td>' +
                    '<td><lable class="label table_label {labelClass}">{classRank}</lable></td>' +
                    '</tr>';

                if(data.complaintList.length>0){
                    $(".complain-task .module-content .data span").html(data.totalCount);
                    $.each(data.complaintList,function(index,item){
                        var _PARAMS = formatCRMData(item);
                        var createDate =new Date(item.createTime);
                        var time =createDate.format("yyyy-MM-dd hh:mm");
                        var $info =$(workInfoHtml.replace("{voucherNumber}" ,item.voucherNumber)
                            .replace("{compId}",item.id)
                            .replace("{createTime}",time)
                            .replace("{createName}",item.callMan)
                            .replace("{createType}",_PARAMS[0])
                            .replace("{createGrade}",_PARAMS[1])
                            .replace("{classRank}",_PARAMS[2])
                            .replace("{labelClass}", item.priorityLevel == "HIGH" ? "label-warning" : "label-default" ));
                        $info.find("td div.complainNumber").attr('title',item.voucherNumber);
                        workOrderList.find("table tbody").append($info);
                    });
                    $(".complain-task .module-content").append(workOrderList);
                    $(".complain-null").css("display","none");
                    // $(".order-null").css("display","block");
                }else{
                    $(".complain-null").css("display","none");
                    $(".order-null").css("display","none");
                }
            }else{
                $(".complain-null").css("display","none");
                $(".order-null").css("display","none");
            }
        },
        error:function () {
            $(".complain-null").css("display","none");
            $(".order-null").css("display","none");
        }
    });
}

//工单任务查看全部
var complaintTaskLogdetail = setMenuDetail( "CRM_01006104","部门工单管理","CRM",CRM_SERVER_NAME + 'crm-web/complaint/complaintDeptManager.action?isUap=true');

function workOrderTaskAll(){
	var complaintTaskAllUrl = CRM_SERVER_NAME + 'crm-web/complaint/complaintDeptManager.action?isUap=true';
    viewTab('部门工单管理',complaintTaskAllUrl,'iframe',null,complaintTaskLogdetail);
}


function formatCRMData(item){
    var _PARAMS = new Array();
    switch (item.typeBusiness) {
        case ("EXPRESS"): _PARAMS[0] = "快递"; break;
        case ("BREAKBULK"): _PARAMS[0] = "零担"; break;

    }

    switch (item.compLevel) {
        case ("LEVEL_1"): _PARAMS[1] = "一级"; break;
        case ("LEVEL_2"): _PARAMS[1] = "二级"; break;
        case ("LEVEL_3"): _PARAMS[1] = "三级"; break;
        case ("LEVEL_4"): _PARAMS[1] = "四级"; break;
        case ("LEVEL_5"): _PARAMS[1] = "五级"; break;
    }

    switch (item.priorityLevel) {
        case ("NORMAL"): _PARAMS[2] = "普通"; break;
        case ("HIGH"): _PARAMS[2] = "优先"; break;

    }
    return _PARAMS;
}